package com.base.myproject.client.system;

import com.base.myproject.client.tools.SendServlet;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;

public class Login {
	protected FormData formData = new FormData("-20");
	final protected FormPanel loginpanel = new FormPanel();
	final protected TextField<String> firstName = new TextField<String>();
	final protected TextField<String> pass = new TextField<String>();
	final protected LayoutContainer layoutcontainer = new LayoutContainer();

	public Login() {
		init();
	}

	public void init() {

	}

	public LayoutContainer createForm() {

		layoutcontainer.setMonitorWindowResize(true);

		layoutcontainer.setStyleAttribute("background",
				"#dfe8f6 url(gwtbase/images/login_bg.jpg) repeat center top");

		layoutcontainer.setHeight(Window.getClientHeight());
		layoutcontainer.setWidth(Window.getClientWidth());

		layoutcontainer.setLayout(new CenterLayout());

		loginpanel.setHeading("登录");
		loginpanel.setFrame(true);
		loginpanel.setWidth(350);

		firstName.setFieldLabel("名字");
		firstName.setName("name");
		firstName.setAllowBlank(false);
		loginpanel.add(firstName, formData);

		pass.setFieldLabel("密码");
		pass.setName("pass");
		pass.setPassword(true);
		pass.setAllowBlank(false);
		loginpanel.add(pass, formData);

		Button b = new Button("确认");
		loginpanel.addButton(b);
		loginpanel.addButton(new Button("取消"));
		Button newuser = new Button("申请新用户");

		loginpanel.setButtonAlign(HorizontalAlignment.CENTER);
		b.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				login();
			}

		});
		newuser.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				Register r = new Register();
				r.getRegDialog().show();
			}
		});
		KeyNav kn = new KeyNav(loginpanel) {
			public void onKeyPress(ComponentEvent ce) {
				if (ce.getKeyCode() == 13) {
					login();
				}
			}
		};
		layoutcontainer.add(loginpanel);
		return layoutcontainer;

	}

	boolean login() {

		if (!loginpanel.isValid()) {
			return false;
		}

		SendServlet send = new SendServlet();
		String str = send.getStringx("gwtbase/BaseServlet", "Action=login&"
				+ firstName.getName() + "=" + firstName.getValue() + "&"
				+ pass.getName() + "=" + pass.getValue());
		System.out.println("-" + str + "-" + Env.LOGINSUCCESS);
		Document doc = XMLParser.parse((String) str);
		NodeList list = doc.getElementsByTagName("root");
		Node node = list.item(0);
		Element elem = (Element) node;
		// 参见服务器的格式
		// 服务器返回一个不重复的uid,客户端每次发送数据的时候到要同时发送uid

		if (elem.getAttribute("status").equals(Env.LOGINSUCCESS)) {

			String id = elem.getAttribute("id");
			String bname = elem.getAttribute("bname");
			String bcode = elem.getAttribute("bcode");
			String title = elem.getAttribute("title");

			if (id != null) {
				Env.setUid(id);
				Env.setEnvbcode(bcode);
				Env.setStatus(Env.BCODELOGIN);
				Env.setEnvname(bname);
				Env.setEnvTitle(title);
				layoutcontainer.setVisible(false);

				com.base.myproject.client.ui.Main m = new com.base.myproject.client.ui.Main();

				RootPanel.get("zgwtest").add(m.initWeiget());
				return true;
			} else {
				MessageBox.alert("提示", "登录出错！", null);
			}
		} else {
			MessageBox.alert("提示", "登录名或者密码录入不正确！", null);
		}
		return false;

	}
}
